﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace VietImage.Models
{
    public partial class LogRequest
    {
        #region Properties

        public User User
        {
            get
            {
                return this.GetUser();
            }
        }

        public Object Object
        {
            get
            {
                return this.GetObjectKind();
            }
        }

        public Action Action
        {
            get
            {
                return this.GetAction();
            }
        }
        #endregion

        #region Methods

        public User GetUser()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Users.Where(p => p.ID == this.ID_User);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Object GetObjectKind()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Objects.Where(p => p.ID == this.ObjectKind);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public Action GetAction()
        {
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();
            var v = _db.Actions.Where(p => p.ID == this.ID_Action);
            if (v.Count() == 0)
                return null;
            return v.First();
        }

        public List<ErrorItem> AcceptRequest(int id_Language)
        {
            List<ErrorItem> el = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            if (ID_Action == 3)
                el = Category.Delete(ID_Object, ID_User, 0, id_Language);
            else if (ID_Action == 2)
            {
                switch (ObjectKind)
                {
                    case 1:
                        {
                            String[] lContent = this.Content.Split('_');
                            DetailImage di = new DetailImage();
                            di.ID_Image = this.ID_Object;
                            di.ID_Language = int.Parse(lContent[1]);
                            di.Name = lContent[2];
                            di.Description = lContent[3];
                            di.Tag = lContent[4];

                            el = DetailImage.ChangeInformation(di, this.ID_User, id_Language);
                            break;
                        }
                    case 2:
                        {
                            String[] lContent = this.Content.Split('_');
                            DetailCategory di = new DetailCategory();
                            di.ID_Category = this.ID_Object;
                            di.ID_Language = int.Parse(lContent[1]);
                            di.Name = lContent[2];
                            di.Description = lContent[3];
                            di.Tag = lContent[4];

                            el.AddRange(DetailCategory.ChangeInformation(di, this.ID_User, id_Language));
                            break;
                        }
                    case 6:
                        {
                            String[] lContent = this.Content.Split('_');
                            DetailUser di = new DetailUser();
                            di.ID_User = this.ID_User;
                            di.ID_Language = int.Parse(lContent[1]);
                            di.Firstname = lContent[2];
                            di.Lastname = lContent[3];
                            di.Address = lContent[4];

                            el = DetailUser.ChangeInformation(di, this.ID_User, id_Language);
                            break;
                        }
                }
            }

            return el;
        }
        #endregion

        #region Static Methods
        public static List<ErrorItem> SendRequest(int id_User, int id_Action, int id_Object, int objectKind, String content, int id_Language)
        {
            List<ErrorItem> el = new List<ErrorItem>();
            VietImageDataClassesDataContext _db = new VietImageDataClassesDataContext();

            int errorMess = _db.SendRequest(id_User, id_Action, id_Object, objectKind, content);
            
            el.Add(new ErrorItem("ErrorMessages", _db.ErrorMessages.Single(p=>p.ID_ErrorMessage == errorMess && p.ID_Language == id_Language).Description));

            return el;
        }
        #endregion
    }
}